<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    // Copyright 2007 Regents of the University of California.  May be used <a name="line.1"></a>
<FONT color="green">002</FONT>    // under the terms of the revised BSD license.  See LICENSING for details.<a name="line.2"></a>
<FONT color="green">003</FONT>    package org.joe_e.reflect;<a name="line.3"></a>
<FONT color="green">004</FONT>    <a name="line.4"></a>
<FONT color="green">005</FONT>    import java.lang.reflect.InvocationHandler;<a name="line.5"></a>
<FONT color="green">006</FONT>    import java.lang.reflect.Modifier;<a name="line.6"></a>
<FONT color="green">007</FONT>    import java.lang.reflect.Proxy;<a name="line.7"></a>
<FONT color="green">008</FONT>    <a name="line.8"></a>
<FONT color="green">009</FONT>    import org.joe_e.Equatable;<a name="line.9"></a>
<FONT color="green">010</FONT>    import org.joe_e.Immutable;<a name="line.10"></a>
<FONT color="green">011</FONT>    import org.joe_e.JoeE;<a name="line.11"></a>
<FONT color="green">012</FONT>    import org.joe_e.Powerless;<a name="line.12"></a>
<FONT color="green">013</FONT>    import org.joe_e.Selfless;<a name="line.13"></a>
<FONT color="green">014</FONT>    <a name="line.14"></a>
<FONT color="green">015</FONT>    /**<a name="line.15"></a>
<FONT color="green">016</FONT>     * The dynamic proxy interface.  This is a wrapper around Java's dynamic proxy<a name="line.16"></a>
<FONT color="green">017</FONT>     * features provided by &lt;code&gt;java.lang.reflect.Proxy&lt;/code&gt;.<a name="line.17"></a>
<FONT color="green">018</FONT>     */<a name="line.18"></a>
<FONT color="green">019</FONT>    public final class Proxies {<a name="line.19"></a>
<FONT color="green">020</FONT>    <a name="line.20"></a>
<FONT color="green">021</FONT>        private Proxies() {}<a name="line.21"></a>
<FONT color="green">022</FONT>    <a name="line.22"></a>
<FONT color="green">023</FONT>        /**<a name="line.23"></a>
<FONT color="green">024</FONT>         * Extract the &lt;code&gt;InvocationHandler&lt;/code&gt; from a Proxy<a name="line.24"></a>
<FONT color="green">025</FONT>         * @param proxy {@link #proxy proxy} instance<a name="line.25"></a>
<FONT color="green">026</FONT>         * @return the invocation handler for the proxy<a name="line.26"></a>
<FONT color="green">027</FONT>         */<a name="line.27"></a>
<FONT color="green">028</FONT>        static public InvocationHandler getHandler(final Proxy proxy) { <a name="line.28"></a>
<FONT color="green">029</FONT>            return Proxy.getInvocationHandler(proxy); <a name="line.29"></a>
<FONT color="green">030</FONT>        }<a name="line.30"></a>
<FONT color="green">031</FONT>    <a name="line.31"></a>
<FONT color="green">032</FONT>        /**<a name="line.32"></a>
<FONT color="green">033</FONT>         * The boot class loader.<a name="line.33"></a>
<FONT color="green">034</FONT>         */<a name="line.34"></a>
<FONT color="green">035</FONT>        static private final ClassLoader boot = Runnable.class.getClassLoader();<a name="line.35"></a>
<FONT color="green">036</FONT>    <a name="line.36"></a>
<FONT color="green">037</FONT>        /**<a name="line.37"></a>
<FONT color="green">038</FONT>         * Constructs a dynamic proxy.<a name="line.38"></a>
<FONT color="green">039</FONT>         * @param handler invocation handler<a name="line.39"></a>
<FONT color="green">040</FONT>         * @param interfaces each implemented interface<a name="line.40"></a>
<FONT color="green">041</FONT>         * @return dynamic proxy<a name="line.41"></a>
<FONT color="green">042</FONT>         * @throws ClassCastException   a restriction on the<a name="line.42"></a>
<FONT color="green">043</FONT>         *                              &lt;code&gt;types&lt;/code&gt; is violated<a name="line.43"></a>
<FONT color="green">044</FONT>         * @throws NullPointerException an argument is &lt;code&gt;null&lt;/code&gt;<a name="line.44"></a>
<FONT color="green">045</FONT>         */<a name="line.45"></a>
<FONT color="green">046</FONT>        static public Object proxy(final InvocationHandler handler,<a name="line.46"></a>
<FONT color="green">047</FONT>                                   final Class&lt;?&gt;... interfaces) <a name="line.47"></a>
<FONT color="green">048</FONT>                                            throws ClassCastException { <a name="line.48"></a>
<FONT color="green">049</FONT>            if (handler == null || interfaces == null) {<a name="line.49"></a>
<FONT color="green">050</FONT>                throw new NullPointerException();<a name="line.50"></a>
<FONT color="green">051</FONT>            }<a name="line.51"></a>
<FONT color="green">052</FONT>            <a name="line.52"></a>
<FONT color="green">053</FONT>            // Determine the classloader.<a name="line.53"></a>
<FONT color="green">054</FONT>            ClassLoader proxyLoader = boot;<a name="line.54"></a>
<FONT color="green">055</FONT>            <a name="line.55"></a>
<FONT color="green">056</FONT>            boolean equatable = false;  // Was the Equatable type claimed?<a name="line.56"></a>
<FONT color="green">057</FONT>            boolean selfless = false;   // Was the Selfless type claimed?<a name="line.57"></a>
<FONT color="green">058</FONT>            boolean immutable = false;  // Was the Immutable type claimed?<a name="line.58"></a>
<FONT color="green">059</FONT>            boolean powerless = false;  // Was the Powerless type claimed?<a name="line.59"></a>
<FONT color="green">060</FONT>            <a name="line.60"></a>
<FONT color="green">061</FONT>            for (final Class&lt;?&gt; i : interfaces) {<a name="line.61"></a>
<FONT color="green">062</FONT>                <a name="line.62"></a>
<FONT color="green">063</FONT>                // Can only implement public interfaces.<a name="line.63"></a>
<FONT color="green">064</FONT>                if (!Modifier.isPublic(i.getModifiers())) {<a name="line.64"></a>
<FONT color="green">065</FONT>                    throw new ClassCastException();<a name="line.65"></a>
<FONT color="green">066</FONT>                }<a name="line.66"></a>
<FONT color="green">067</FONT>                <a name="line.67"></a>
<FONT color="green">068</FONT>                // Perform Joe-E auditor checks.<a name="line.68"></a>
<FONT color="green">069</FONT>                if (!powerless &amp;&amp; JoeE.isSubtypeOf(i, Powerless.class)) {<a name="line.69"></a>
<FONT color="green">070</FONT>                    if (JoeE.instanceOf(handler, Powerless.class)) {<a name="line.70"></a>
<FONT color="green">071</FONT>                        powerless = true;<a name="line.71"></a>
<FONT color="green">072</FONT>                        immutable = true;<a name="line.72"></a>
<FONT color="green">073</FONT>                    } else {<a name="line.73"></a>
<FONT color="green">074</FONT>                        throw new ClassCastException();<a name="line.74"></a>
<FONT color="green">075</FONT>                    }<a name="line.75"></a>
<FONT color="green">076</FONT>                }<a name="line.76"></a>
<FONT color="green">077</FONT>                if (!immutable &amp;&amp; JoeE.isSubtypeOf(i, Immutable.class)) {<a name="line.77"></a>
<FONT color="green">078</FONT>                    if (JoeE.instanceOf(handler, Immutable.class)) {<a name="line.78"></a>
<FONT color="green">079</FONT>                        immutable = true;<a name="line.79"></a>
<FONT color="green">080</FONT>                    } else {<a name="line.80"></a>
<FONT color="green">081</FONT>                        throw new ClassCastException();<a name="line.81"></a>
<FONT color="green">082</FONT>                    }<a name="line.82"></a>
<FONT color="green">083</FONT>                }<a name="line.83"></a>
<FONT color="green">084</FONT>                if (!equatable &amp;&amp; JoeE.isSubtypeOf(i, Equatable.class)) {<a name="line.84"></a>
<FONT color="green">085</FONT>                    // No additional checks are needed here, as we know that Proxy<a name="line.85"></a>
<FONT color="green">086</FONT>                    // is not Selfless.  An Equatable Proxy can have an identity-<a name="line.86"></a>
<FONT color="green">087</FONT>                    // based equals() method if it so chooses by casting the Proxy<a name="line.87"></a>
<FONT color="green">088</FONT>                    // object to Equatable.<a name="line.88"></a>
<FONT color="green">089</FONT>                    if (selfless) { <a name="line.89"></a>
<FONT color="green">090</FONT>                        throw new ClassCastException();<a name="line.90"></a>
<FONT color="green">091</FONT>                    }<a name="line.91"></a>
<FONT color="green">092</FONT>                    equatable = true;<a name="line.92"></a>
<FONT color="green">093</FONT>                }<a name="line.93"></a>
<FONT color="green">094</FONT>                if (!selfless &amp;&amp; JoeE.isSubtypeOf(i, Selfless.class)) {<a name="line.94"></a>
<FONT color="green">095</FONT>                    // No additional checks are needed here, because Object's<a name="line.95"></a>
<FONT color="green">096</FONT>                    // equals() method is not available from the invocation handler<a name="line.96"></a>
<FONT color="green">097</FONT>                    // and Proxy does not expose this method.  Dynamic proxies are<a name="line.97"></a>
<FONT color="green">098</FONT>                    // thus the only Selfless user class whose direct supertype is<a name="line.98"></a>
<FONT color="green">099</FONT>                    // neither a Selfless class nor Object.<a name="line.99"></a>
<FONT color="green">100</FONT>                    if (equatable) { <a name="line.100"></a>
<FONT color="green">101</FONT>                        throw new ClassCastException();<a name="line.101"></a>
<FONT color="green">102</FONT>                    }<a name="line.102"></a>
<FONT color="green">103</FONT>                    selfless = true;<a name="line.103"></a>
<FONT color="green">104</FONT>                }<a name="line.104"></a>
<FONT color="green">105</FONT>                <a name="line.105"></a>
<FONT color="green">106</FONT>                final ClassLoader interfaceLoader = i.getClassLoader();<a name="line.106"></a>
<FONT color="green">107</FONT>    <a name="line.107"></a>
<FONT color="green">108</FONT>                // TODO: This will change if there is runtime info on banned<a name="line.108"></a>
<FONT color="green">109</FONT>                // interfaces or if a Library interface is added.<a name="line.109"></a>
<FONT color="green">110</FONT>                // if (interfaceLoader == boot &amp;&amp; i != Runnable.class) { <a name="line.110"></a>
<FONT color="green">111</FONT>                //    throw new ClassCastException();<a name="line.111"></a>
<FONT color="green">112</FONT>                //}<a name="line.112"></a>
<FONT color="green">113</FONT>        <a name="line.113"></a>
<FONT color="green">114</FONT>                // Prefer any classloader over the bootstrap classloader.<a name="line.114"></a>
<FONT color="green">115</FONT>                if (proxyLoader == boot) {<a name="line.115"></a>
<FONT color="green">116</FONT>                    proxyLoader = interfaceLoader;<a name="line.116"></a>
<FONT color="green">117</FONT>                }<a name="line.117"></a>
<FONT color="green">118</FONT>            }<a name="line.118"></a>
<FONT color="green">119</FONT>            try {<a name="line.119"></a>
<FONT color="green">120</FONT>                return Proxy.newProxyInstance(proxyLoader, interfaces, handler);<a name="line.120"></a>
<FONT color="green">121</FONT>            } catch (final IllegalArgumentException e) {<a name="line.121"></a>
<FONT color="green">122</FONT>                throw new ClassCastException(e.getMessage());<a name="line.122"></a>
<FONT color="green">123</FONT>            }<a name="line.123"></a>
<FONT color="green">124</FONT>        }<a name="line.124"></a>
<FONT color="green">125</FONT>    <a name="line.125"></a>
<FONT color="green">126</FONT>        /**<a name="line.126"></a>
<FONT color="green">127</FONT>         * Returns &lt;code&gt;true&lt;/code&gt; if the argument is an interface that can be<a name="line.127"></a>
<FONT color="green">128</FONT>         * implemented by a Proxy using &lt;code&gt;proxy()&lt;/code&gt;.<a name="line.128"></a>
<FONT color="green">129</FONT>         * @param type  candidate interface<a name="line.129"></a>
<FONT color="green">130</FONT>         * @return &lt;code&gt;true&lt;/code&gt; if a Joe-E type, else &lt;code&gt;false&lt;/code&gt;<a name="line.130"></a>
<FONT color="green">131</FONT>         */<a name="line.131"></a>
<FONT color="green">132</FONT>        static public boolean isImplementable(final Class&lt;?&gt; type) {<a name="line.132"></a>
<FONT color="green">133</FONT>            return type.isInterface() &amp;&amp; Modifier.isPublic(type.getModifiers());<a name="line.133"></a>
<FONT color="green">134</FONT>            <a name="line.134"></a>
<FONT color="green">135</FONT>            // return Runnable.class == type ||<a name="line.135"></a>
<FONT color="green">136</FONT>            //       (type.isInterface() &amp;&amp; type.getClassLoader() != boot &amp;&amp;<a name="line.136"></a>
<FONT color="green">137</FONT>            //        Modifier.isPublic(type.getModifiers())); <a name="line.137"></a>
<FONT color="green">138</FONT>            // Can't consult taming database as it doesn't have proper info<a name="line.138"></a>
<FONT color="green">139</FONT>            // TODO: This will change if there are interfaces the implementation of<a name="line.139"></a>
<FONT color="green">140</FONT>            // which must be banned (e.g. Library)<a name="line.140"></a>
<FONT color="green">141</FONT>        }<a name="line.141"></a>
<FONT color="green">142</FONT>    }<a name="line.142"></a>




























































</PRE>
</BODY>
</HTML>
